/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kz.shange.security;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.kz.shange.common.KzTemplate;
import org.kz.shange.models.Author;
import org.kz.shange.models.IAuthorDao;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "authorMgr")
@ViewScoped
public class AuthorMgr extends KzTemplate implements Serializable {

    private Author curAuthor;
    @ManagedProperty(name = "authDao", value = "#{authorDao}")
    private IAuthorDao authDao;

    @PostConstruct
    public void construct() {
        this.curAuthor = new Author();
    }

    public void regedit() {
        if (!this.validate(curAuthor)) {
            return;
        }
        this.authDao.saveAuthor(curAuthor);
    }

    private boolean validate(Author a) {
        Author tmp = this.authDao.findAuthor(a.getAccount());
        if ((tmp != null) && (tmp.getAccount().equals(a.getAccount()))) {
            super.addErrorMessage("登录帐号已被注册，请换另一个帐号！");
        }
        super.addErrorMessage(super.validateString("登录帐号", a.getAccount(), 20));
        super.addErrorMessage(super.validateString("登录密码", a.getPassword(), 20));
        super.addErrorMessage(super.validateString("显示名称", a.getName(), 20));
        return !super.isValidateFailed();
    }

//<editor-fold defaultstate="collapsed" desc="property">
    public Author getCurAuthor() {
        return curAuthor;
    }

    public void setCurAuthor(Author curAuthor) {
        this.curAuthor = curAuthor;
    }

    public IAuthorDao getAuthDao() {
        return authDao;
    }

    public void setAuthDao(IAuthorDao authDao) {
        this.authDao = authDao;
    }
//</editor-fold>
}
